// 作用先运行webpack-dev-server然后再运行electron
process.env.NODE_ENV = 'development'
const electron = require('electron')
const webpackDevServer = require('webpack-dev-server');
const webpack = require('webpack');

const { spawn } = require('child_process')
const path = require('path')

const config = require('../webpack.config.js');
const log = require('electron-log');
const options = {
    hot: true,
    host: 'localhost',
    // clientLogLevel: 'warning', // 启用错误日志
    compress: true, // 启用压缩
    after(app, ctx) {
        ctx.middleware.waitUntilValid(() => {
            // 打包完成启动electron
            // 开启之后准备打开electron
            console.log('打包完毕......')
            console.log('准备开启electron应用！')
            let childProcess = spawn(electron, ['main.js'], {
                env: {
                    NODE_ENV: 'development'
                }
            })
            childProcess.stdout.on('data', data => {
                log.info(data.toString())
            })
            childProcess.on('close', () => {
                process.exit()
            })
        })
    }
};

webpackDevServer.addDevServerEntrypoints(config, options);
const compiler = webpack(config);
const server = new webpackDevServer(compiler, options);

server.listen(8080, 'localhost', () => {
    console.log('启动热更新服务器正常！')
    console.log('开始打包......')
})